$(function () {
    //需求1：初始化文章分类-因为后面还要用，所以封装成函数
    initArtCateList();
    let  layer = layui.layer
    function initArtCateList() {
        axios({
            method: 'GET',
            url: '/my/article/cates',
        }).then(res => {
            // console.log(res.data);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message);
            }
            //利用数组，拼接字符串
            let arr = [];
            res.data.data.forEach(ele => {
                arr.push(`
                    <tr>
                        <td>${ ele.Id }</td>
                        <td>${ ele.name }</td>
                        <td>${ ele.alias }</td>
                        <td>
                        <button data-id="${ ele.Id }" class="btn-edit layui-btn layui-btn-xs">编辑</button>
                        <button class="layui-btn btn-del layui-btn-xs layui-btn-danger">删除</button></td>
                    </tr>
                `);
            });
            $('tbody').empty().html(arr)
        });
    };
    //需求2：点击按钮，弹出对话框
    let indexAdd = 0;
    $("#addBtn").on('click', function () {
        indexAdd = layer.open({
            type: 1,
            area: ['500px', '260px'],
            title: '添加文章类别'
            ,content: `
            <form id="addForm" class="layui-form" action="">
            <div class="layui-form-item">
              <label class="layui-form-label">分类名称</label>
              <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                  class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">分类别名</label>
              <div class="layui-input-block">
                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                  class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                
              </div>
            </div>
          </form>
            `
          });    
    });
    //需求3：添加文章分类
    $("body").on('submit', '#addForm', function (e) {
        e.preventDefault();
        axios({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize()
        }).then(res => {
            // console.log(res)
            if (res.data.status !== 0) {
                return layer.msg(res.data.message);
            };
            layer.msg('添加成功！');
            //重新渲染页面
            initArtCateList();
            //关闭对话框
            layer.close(indexAdd);
        })
    });
    //需求4：点击修改按钮，弹出对话框
    let indexEdit = 0;
    $("tbody").on('click', '.btn-edit', function () {
        indexEdit = layer.open({
            type: 1,
            area: ['500px', '260px'],
            title: '修改文章类别'
            ,content: `
            <form id="editForm" class="layui-form" lay-filter="editForm" action="">
            <!-- 隐藏域,将来修改数据的时候使用 -->
            <input type="hidden" name="Id">
            <div class="layui-form-item">
              <label class="layui-form-label">分类名称</label>
              <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                  class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">分类别名</label>
              <div class="layui-input-block">
                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                  class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即修改</button>
                
              </div>
            </div>
          </form>
            `
        }); 
        let id = $(this).attr('data-id');
        axios({
            method: 'GET',
            ///接口文档中的‘:’代表是可以变化的
            //传值的时候 / 不能省略 ‘:’必须删除
            url: '/my/article/cates/' + id,
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message);
            }
            //成功后渲染
            layui.form.val("editForm", res.data.data);
        })
    }); 
    //需求5：修改文章分类
    $("body").on('submit', '#editForm', function (e) {
        e.preventDefault();
        axios({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize()
        }).then(res => {
            // console.log(res)
            if (res.data.status !== 0) {
                return layer.msg(res.data.message);
            };
            layer.msg('修改成功！');
            //重新渲染页面
            initArtCateList();
            //关闭对话框
            layer.close(indexEdit);
        })
    });
    //需求6:删除文章分类
    $("tbodoy").on('click', ".btn-del", function () {
        //获取自定义属性值
        let id = $(this).attr("data-id");
        layer.confirm('是否确认？', {icon: 3, title: '提示'}, function (index) {
            axios({
                method: 'GET',
                url: '/my/article.deletecate/' + id
            }).then(res => {
                // console.log(res.data);
                if (res.data.status !== 0) {
                    return layer.msg(res.data.message);
                }
                layer.msg('恭喜，删除成功！');
                initArtCateList();
            });
            layer.close(index);
        })
    })
})